package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 菜品总览：获取起售和停售菜品的数目
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer sold=dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued=dishMapper.countByStatus(StatusConstant.DISABLE);
        return new DishOverViewVO(sold,discontinued);
    }

    /**
     * 套餐总览：获取起售和停售的套餐数目
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeal() {
        Integer sold=setmealMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued=setmealMapper.countByStatus(StatusConstant.DISABLE);
        return new SetmealOverViewVO(sold,discontinued);
    }

    /**
     * 订单总览：获取各个状态的订单数目
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        Integer waitingOrders=orderMapper.countByStatus(Orders.TO_BE_CONFIRMED);
        Integer deliveryOrders=orderMapper.countByStatus(Orders.CONFIRMED);
        Integer completedOrders=orderMapper.countByStatus(Orders.COMPLETED);
        Integer cancelledOrders=orderMapper.countByStatus(Orders.CANCELLED);
        Integer allOrders=orderMapper.countAll();
        return new OrderOverViewVO(waitingOrders,deliveryOrders,completedOrders,cancelledOrders,allOrders);
    }

    /**
     * 获取今日数据：营业额，有效订单，订单完成率，平均客单价，新增用户
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDate today) {
        Integer turnover=orderMapper.todayTurnover(today); //总营业额
        turnover=turnover==null?0:turnover;
        List<OrderPerDateVO> orderPerDateVOs=orderMapper.getOrderPerDate(today,today);
        Integer completedOrders=0;
        Integer allOrder=0;
        Double avgAmount=0.0;
        Double completedRate=0.0;
        Integer newUsers=0;
        if(orderPerDateVOs!=null && !orderPerDateVOs.isEmpty()){
            completedOrders= Math.toIntExact(orderPerDateVOs.get(0).getValuedOrderSum()); //有效订单
            allOrder=Math.toIntExact(orderPerDateVOs.get(0).getOrderSum()); //总订单
            if(completedOrders>0){
                avgAmount=((double) turnover)/completedOrders;
                completedRate=((double) completedOrders)/allOrder;
            }
        }
        List<UserVO> userVOS=userMapper.getUserAddition(today,today); //今日增长用户
        if(userVOS!=null&&!userVOS.isEmpty()){
            newUsers=Math.toIntExact(userVOS.get(0).getNewAddition());
        }
        return new BusinessDataVO((double) turnover,completedOrders,completedRate,avgAmount,newUsers);
    }
}
